package cn.net.autocode.platform.design.handle;

import cn.hutool.core.util.ClassUtil;
import cn.hutool.core.util.StrUtil;
import cn.net.autocode.platform.design.vo.AppInfo;
import cn.net.autocode.platform.design.vo.DeveloperInfo;
import com.alibaba.fastjson2.JSONObject;
import lombok.Getter;
import lombok.extern.java.Log;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Base64;

@Log
public class AutoCodeHandle {

    /**
     * -- GETTER --
     *  获取项目信息
     */
    @Getter
    private static AppInfo appInfo;

    private static DeveloperInfo developerInfo;

    private static String appName;

    static{
        try {
            String filePath = ClassUtil.getClassPath()+"/license.lic";

            Path path = Paths.get(filePath);
            String content = Files.readString(path);

            //进行解密
            JSONObject json = PlatformJieMiUtil.decrypt(content);
            appInfo = new AppInfo();
            appInfo.setAppid(json.getString("appid"));
            appInfo.setAppName(json.getString("appName"));
            appInfo.setCode(json.getString("code"));
            appInfo.setLicense(json.getString("license"));
        } catch (IOException e) {
            log.info(e.getMessage());
        }
    }


    /**
     * 获取开发者信息
     */
    public static DeveloperInfo getDeveloperInfo(){
        if(developerInfo==null){
            try {
                String filePath = System.getProperty("user.home")+"/"+ new String(Base64.getDecoder().decode("YWN3aWt3Mjg5NjU3aDJkYi5saWNlbnNl"), StandardCharsets.UTF_8);
                Path path = Paths.get(filePath);
                String content = Files.readString(path);
                JSONObject json = JSONObject.parseObject(content);

                if(StrUtil.isBlank(json.getString("licenseToken"))){
                    return null;
                }

                developerInfo = new DeveloperInfo();
                developerInfo.setToken(json.getString("licenseToken"));
                developerInfo.setEmail(json.getString("email"));
                developerInfo.setPhone(json.getString("phone"));
                developerInfo.setFullname(json.getString("fullname"));
                developerInfo.setUsername(json.getString("username"));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return developerInfo;
    }

    public static void writeDeveloperInfo(JSONObject jsonObject){
        String filePath = System.getProperty("user.home")+"/"+ new String(Base64.getDecoder().decode("YWN3aWt3Mjg5NjU3aDJkYi5saWNlbnNl"),StandardCharsets.UTF_8);
        try {
            Path path = Paths.get(filePath);
            String content = jsonObject.toJSONString();
            Files.writeString(path, content);
            if (developerInfo == null){
                developerInfo = new DeveloperInfo();
            }
            developerInfo.setToken(jsonObject.getString("licenseToken"));
            developerInfo.setEmail(jsonObject.getString("email"));
            developerInfo.setPhone(jsonObject.getString("phone"));
            developerInfo.setFullname(jsonObject.getString("fullname"));
            developerInfo.setUsername(jsonObject.getString("username"));

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static String getAppName(){
        if (StrUtil.isBlank(AutoCodeHandle.appName)){
            URL resourceUrl = Thread.currentThread().getContextClassLoader().getResource("");
            if (resourceUrl!= null) {
                String path = resourceUrl.getPath();
                path = path.replace("target/classes","");
                String[] paths = path.split("/");
                for(int i=paths.length-1;i>0;i--){
                    String name = paths[i];
                    if(name!=null&&!name.isBlank()){
                        AutoCodeHandle.appName = name.toLowerCase().replace("-","").replace("_","");
                        return AutoCodeHandle.appName;
                    }
                }
            }
        }
        return AutoCodeHandle.appName;
    }


}
